import { _decorator, Button, Collider2D, Component, Contact2DType, instantiate, Label, Node, ProgressBar, random, Vec3} from 'cc';

import { BattleManager } from './BattleManager';
import { EntityManager } from './Base/EntityManager';
import { PlayerStateMachine } from './PlayerStateMachine';
import { EEvent, EGroup, EState, EntityType, EPlayerType, ESkill, EBuff, ESkillType, EBaseAttribute, EWuxing } from './Config/Enum';
import { AudioManager, Core } from './Core/Core';
import { SkillManager } from './Skill/SkillManager';
import { EnemyManager } from './EnemyManager';
import ObjectPoolManager from './Core/ObjectPoolManager';
import { PlayerData } from './PlayerData';
import { SkillData } from './Skill/SkillData';
//import { Buff, BuffData } from './BuffData';
import { buffConfig, EPassiveSkill } from './Config/SkillConfig';
import { Skill } from './Skill/Skill';
import { Util } from './Util';
import { ShaolinSkill1 } from './Skill/ShaolinSkill1';
import { innerPowerEnhance } from './Config/Config';
import { TangmenSkill1 } from './Skill/TangmenSkill1';
import { GaibangSkill2 } from './Skill/GaibangSkill2';
import { WudangSkill1 } from './Skill/WudangSkill1';
import { GaibangSkill3 } from './Skill/GaibangSkill3';
import { GaibangSkill1 } from './Skill/GaibangSkill1';
import { ShaolinSkill2 } from './Skill/ShaolinSkill2';
import { TangmenSkill2 } from './Skill/TangmenSkill2';
import { TangmenSkill3 } from './Skill/TangmenSkill3';
import { EmeiSkill1 } from './Skill/EmeiSkill1';
import { EmeiSkill2 } from './Skill/EmeiSkill2';
import { EmeiSkill3 } from './Skill/EmeiSkill3';
import { WudangSkill2 } from './Skill/WudangSkill2';
import { WudangSkill3 } from './Skill/WudangSkill3';
import { EAffixType } from './Item/Item';

const { ccclass, property } = _decorator;



@ccclass('PlayerManager')
export class PlayerManager extends EntityManager {

    private _speedFactor: number = 1; // 添加速度因子


    gm:BattleManager
    playerType: EPlayerType

    wuxing: EWuxing = EWuxing.Jin; // 五行属性，默认无
    

    // 玩家基础属性,力量智力敏捷体质内力等
    strength: number = 10; //力量
    agility: number = 10; //敏捷
    intelligence: number = 10; //智力
    innerPower: number = 10; //内力
    vitality : number = 10; //体质
    direction: Vec3 = new Vec3(0, 0, 0); // 玩家当前方向



    // 玩家中级属性 受基础属性影响
    ap: number = 10; // attack power
    dp: number = 10; // defense power
    ats: number = 0.1; // attack speed
    hp: number = 0; // health points
    speed: number = 0; // speed
    attackRange: number = 0; // attack range
    attackSpeed: number = 0; // attack speed
    criticalChance: number = 0;
    criticalDamage: number = 150;
    wuxingEnhance: number = 0.1; // 五行增强
    skillEnhance: number = 0; // 武学增强
    sheild: number = 0; // 盾
    accuracy: number = 0; // 命中率
    penetrateCount: number = 0; // 穿透数量
    penetratePercent: number = 0; // 穿透百分比
    ComboChance: number = 0; // 连击率
    //evasion: number = 0; // 闪避率
    rageRegen: number = 0; // 怒气回复速度
    skillLevel:number
    skill1Level:number
    skill2Level:number
    skill3Level:number
    ignoreDefense:number
    moreExp:number
    moreReown:number
    moreCoin:number
    MF:number
    dodge:number

    //其他属性
    moneyPercent
    renow

    // 玩家固定属性
    //ragePerAttack: number = 5; // 每次攻击增加的怒气值
    rageMax: number = 100; // 最大怒气值
    rage: number = 0; // 当前怒气值

    // 模块化数据 玩家武学、装备、背包等
    skillData: SkillData
    passiveSKill:EPassiveSkill[]
    //buffData: BuffData
    // equipmentData: EquipmentData

    // 玩家局内属性
    // damageThisGame: number = 0; // 伤害
    isInvincible = false
    canAttack: boolean = true;


    
    skills:Map<ESkill, Skill>
    nearestEnemy: Node;



    playerData: PlayerData  
    // 装备管理器
    // public em: EquipmentManager;

    skillReady: Set<ESkill> = new Set(); // 就绪武学列表
    isSample = false

    avaliableSkills: ESkill[] = []
    skillCDMap: Map<ESkill, number> = new Map();

    skill1:ESkill
    skill2:ESkill
    skill3:ESkill


    ndRageBar: Node
    rageBar: ProgressBar
    UltimateSkillButton: Button
    attackCheckTimer: number = 0;
    CDR: number =0

    wuxingAffixes


    

    // 只用于显示一个动画，不需要其他逻辑
    initState(playerType: EPlayerType){
        console.log("PlayerManager initState", playerType);
        this.playerType = playerType
        this.fsm = this.node.addComponent(PlayerStateMachine)
        this.fsm.init(playerType,0.5,true)
        // 延迟一帧设置初始状态，确保状态机初始化完成

    }
    

    init(playerType: EPlayerType){
        console.log("PlayerManager init", playerType);

        this.node.setScale(1.5, 1.5, 1);
        // // 从playerData装载玩家数据
        this.playerData = Core.Instance.players.get(playerType);

        // 从playerData深拷贝玩家数据
        //this.playerData = JSON.parse(JSON.stringify(Core.Instance.players.get(playerType))) as PlayerData;


        this.playerType = playerType
        this.fsm = this.node.addComponent(PlayerStateMachine)
        this.fsm.init(playerType, this.playerData.attackSpeed)
        this.state = EState.Idle
        

        // 复制一份武学数据
        this.skills = new Map();
        for (const [key, skill] of this.playerData.skillData.skills) {
            this.skills.set(key, skill.clone());
        }
        this.passiveSKill = this.playerData.skillData.passiveSkills
        //this.buffData = this.playerData.buffData

        // 属性
        this.initAttribute(playerType);
        console.log("----------------------------------------------------");
        this.printAttribute();
        console.log("----------------------------------------------------");

        // 查看this.skills每个武学的cd
        for (const skill of this.skills.values()) {
            console.log("武当符文1效果 PlayerManager init skill", this.playerType, skill.type, "cd", skill.cd);
        }

        this.skill1 = this.playerType + "Skill1" as ESkill
        this.skill2 = this.playerType + "Skill2" as ESkill
        this.skill3 = this.playerType + "Skill3" as ESkill

        this.wuxingAffixes = this.playerData.equipmentStats_wuxingStats || {};
        // console.log("五行词缀生效啊PlayerManager init this.playerData.equipmentStats", this.playerData.equipmentStats_wuxingStats);
        // console.log("五行词缀生效啊PlayerManager init wuxingAffixes", this.wuxingAffixes);
        console.log("PlayerManager init wuxingAffixes", this.wuxingAffixes);
    // 提前计算各武学伤害值， 没有计算五行、暴击、防御，（这里还会检查五行词缀，有没有固定伤害）
        this.calculateSkillDamage();






        for (const skill of this.skills.values()) {
            // console.log("skill", skill);
            // 如果skill的level为0，则不加入武学列表
            if (skill.level === 0) {
                continue;
            }
            // 重新计算CD，因为有CDR属性
            skill.cd = Util.roundToTwo(skill.cd * (1 - this.CDR));
            this.skillCDMap.set(skill.type, skill.cd);  // {ESkill.ShaolinSkill1:1, ESkill.ShaolinSkill2:3, ESkill.ShaolinSkill3:5}
        }

        // console.log("skillCDMap", this.skillCDMap);
        this.ndRageBar = this.node.getChildByName("Rage");
        this.rageBar = this.node.getChildByName("Rage").getComponent(ProgressBar);
        this.UltimateSkillButton = this.node.getChildByName("Button_UltimateSkill").getComponent(Button);
        this.UltimateSkillButton.interactable = false;

        // 测试用
        // this.em.applyEquipmentEffects();

        // 如果大招等级为0，就不显示怒气条
        if (this.skills.get(this.skill3).level === 0) {
            this.ndRageBar.active = false;
            this.UltimateSkillButton.node.active = false;
        } else {
            this.ndRageBar.active = true;
            this.UltimateSkillButton.node.active = true;
        }


        this.checkPassiveSkill()


    }

    initAttribute(playerType: EPlayerType) {



        // 基础属性
        this.strength = this.playerData.strength
        this.agility = this.playerData.agility
        this.intelligence = this.playerData.intelligence
        this.innerPower = this.playerData.innerPower
        this.vitality = this.playerData.vitality
        // 中级属性
        this.ap = this.playerData.ap
        this.dp = this.playerData.dp
        this.hp = this.playerData.hp
        this.accuracy = this.playerData.accuracy
        this.speed = this.playerData.speed
        this.attackSpeed = this.playerData.attackSpeed
        this.criticalDamage = this.playerData.criticalDamage
        this.attackRange = this.playerData.attackRange
        this.criticalChance = this.playerData.criticalChance
        this.skillEnhance = this.playerData.skillEnhance
        this.wuxing = this.playerData.wuxing
        this.wuxingEnhance = this.playerData.wuxingEnhance
        this.skillLevel = this.playerData.skillLevel
        this.skill1Level = this.playerData.skill1Level
        this.skill2Level = this.playerData.skill2Level
        this.skill3Level = this.playerData.skill3Level
        this.ignoreDefense = this.playerData.ignoreDefense
        this.ComboChance = this.playerData.ComboChance
        this.rageRegen = this.playerData.rageRegen

        this.moreCoin = this.playerData.moreCoin
        this.moreExp = this.playerData.moreExp
        this.moreReown = this.playerData.moreReown
        this.MF = this.playerData.MF
        this.CDR = this.playerData.CDR
        this.dodge = this.playerData.dodge
        


        //this.ragePerAttack = this.playerData.ragePerAttack
        this.rageMax = this.playerData.rageMax
        this.rage = 0
        this.penetratePercent = this.playerData.penetratePercent
        this.penetrateCount = this.playerData.penetrateCount


    }

    // 提前计算各武学伤害值， 没有计算五行、暴击、防御，（这里还会检查五行词缀，有没有固定伤害）
    calculateSkillDamage(){

        console.log("PlayerManager calculateSkillDamage this.wuxingAffixes", this.wuxingAffixes);
        // 提前计算各武学伤害值， 没有计算五行、暴击、防御
        let extraDamage = 0;
        // 检查所有可能的属性伤害加成
                            console.log("pm calculateSkillDamage skill damage extraDamage0", this.wuxingAffixes[EAffixType.DamageIncrease25Strength]);
        if (this.wuxingAffixes[EAffixType.DamageIncrease25Strength] !== undefined) {
                    console.log("pm calculateSkillDamage skill damage extraDamage1", extraDamage);
            extraDamage += this.wuxingAffixes[EAffixType.DamageIncrease25Strength] * this.strength;
                    console.log("pm calculateSkillDamage skill damage extraDamage2", extraDamage);
        }
        
        if (this.wuxingAffixes[EAffixType.DamageIncrease25Agility] !== undefined) {
                    console.log("pm calculateSkillDamage skill damage extraDamage3", extraDamage);
            extraDamage += this.wuxingAffixes[EAffixType.DamageIncrease25Agility] * this.agility;
                    console.log("pm calculateSkillDamage skill damage extraDamage4", extraDamage);
        }
        
        if (this.wuxingAffixes[EAffixType.DamageIncrease25Intelligence] !== undefined) {
                    console.log("pm calculateSkillDamage skill damage extraDamage5", extraDamage);
            extraDamage += this.wuxingAffixes[EAffixType.DamageIncrease25Intelligence] * this.intelligence;
                    console.log("pm calculateSkillDamage skill damage extraDamage6", extraDamage);
        }


        console.log("pm calculateSkillDamage skill damage extraDamage7", extraDamage);



        for (const skill of this.skills.values()) {
            //skill.calculateDamage(this.ap, this.dp, this.primaryAttribute, this.wuxing);
            skill.damage = (skill.ap/100*this.ap)*(this.skillEnhance+this.innerPower*innerPowerEnhance+1)
            console.log("pm calculateSkillDamage skill damage", skill.type, skill.damage);
        }

    }

    onLoad(): void {

    }

    onEnable(): void {
        let collider = this.node.getComponent(Collider2D);
        collider?.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        collider?.on(Contact2DType.END_CONTACT, this.onEndContact, this);

        // 监听动画播放完毕事件
        this.node.on("PlayerAttackAnimationFinished", this.onAttackAnimationFinished, this);

        //监听JoyStick
        // Core.Instance.event.on(EEvent.PLAYER_MOVE, this.onPlayerMove, this);

        //监听PlayerDataChange
        Core.Instance.event.on(EEvent.APChange, this.onPlayerAPChange, this);
        //Core.Instance.event.on(EEvent.PlayerBuff, this.onPlayerBuff, this);
        //Core.Instance.event.on(EEvent.PlayerUltimateReady, this.onPlayerUltimateReady, this);




    }

    onDisable(): void {
        let collider = this.node.getComponent(Collider2D);
        collider?.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        collider?.off(Contact2DType.END_CONTACT, this.onEndContact, this);

        //Core.Instance.event.off(EEvent.PLAYER_MOVE, this.onPlayerMove, this);
        Core.Instance.event.off(EEvent.APChange, this.onPlayerAPChange, this);
        //Core.Instance.event.off(EEvent.PlayerBuff, this.onPlayerBuff, this);
        //Core.Instance.event.off(EEvent.PlayerUltimateReady, this.onPlayerUltimateReady, this);

        
    }

    start() {

    }

// 每个player有自己的数据，自己的buff列表
    update(dt: number) {

        // 应用速度因子
        const scaledDt = dt * this._speedFactor;



        // 检查有没有到攻击范围，如果有就开始自动攻击
        if (this.state === EState.IdleFront) {
            return;
        }
    // 降低攻击检测频率：每0.3秒检查一次
    this.attackCheckTimer += scaledDt;
    //console.log("this.attackCheckTimer1",this.attackCheckTimer)
    if (this.attackCheckTimer >= 0.3) {
        //console.log("this.attackCheckTimer2",this.attackCheckTimer)
        this.checkEnemyInRange();
        this.attackCheckTimer = 0;
    }
        // 遍历武学CD列表，减去dt，如果小于等于0就删除这个武学，加入就绪武学列表
        this.skillCDMap.forEach((cd, skill) => {
            const newCD = cd - scaledDt;
            if (newCD <= 0) {
                this.skillCDMap.delete(skill);
                this.skillReady.add(skill); // 使用Set自动去重
            } else {
                this.skillCDMap.set(skill, newCD);
            }
        });

    }


        // 添加设置速度的方法
    setSpeedFactor(factor: number) {
        this._speedFactor = factor;
    }
    //     // 当装备变更时更新属性
    // public onEquipmentChanged() {
    //     // 重置基础属性
    //     this.resetBaseStats();
        
    //     // 重新应用装备效果
    //     this.em.applyEquipmentEffects();
    // }

    printAttribute() {
        console.log("PlayerManager printAttribute strength", this.strength, this.agility, this.intelligence);   
        console.log("PlayerManager printAttribute", this.playerType);
        console.log("PlayerManager printAttribute ap", this.ap);
        console.log("PlayerManager printAttribute dp", this.dp);
        console.log("PlayerManager printAttribute hp", this.hp);
        console.log("PlayerManager printAttribute speed", this.speed);
        console.log("PlayerManager printAttribute attackRange", this.attackRange);
        console.log("PlayerManager printAttribute attackSpeed攻击间隔计算", this.attackSpeed);
        console.log("PlayerManager printAttribute criticalChance", this.criticalChance);
        console.log("PlayerManager printAttribute criticalDamage", this.criticalDamage);
        //console.log("ragePerAttack", this.ragePerAttack);
        console.log("rageMax", this.rageMax);
        console.log("rageRegen", this.rageRegen);
        console.log("PlayerManager printAttribute skill1Level", this.skill1Level);
        console.log("PlayerManager printAttribute skill2Level", this.skill2Level);
        console.log("PlayerManager printAttribute skill3Level", this.skill3Level);
        console.log("PlayerManager printAttribute skillLevel", this.skillLevel);
        console.log("PlayerManager printAttribute ComboChance", this.ComboChance);
        console.log("PlayerManager printAttribute penetratCount", this.penetrateCount);
        console.log("PlayerManager printAttribute penetratePercent", this.penetratePercent);


        //console.log("PlayerManager printAttribute primaryAttribute", this.primaryAttribute);
        console.log("PlayerManager printAttribute wuxingEnhance", this.wuxingEnhance);
        console.log("PlayerManager printAttribute skillLevel", this.skillLevel);
        console.log("PlayerManager printAttribute wuxing", this.wuxing);
        console.log("PlayerManager printAttribute accuracy", this.accuracy);
        console.log("PlayerManager printAttribute CDR", this.CDR);
        console.log("PlayerManager printAttribute dodge", this.dodge);

        // 打印playerdata里的skill和this.skill的区别
        console.log("PlayerManager printAttribute skill", this.playerData.skillData.skills);
        console.log("PlayerManager printAttribute this.skill", this.skills);
        


    }

    addRage(value){
        this.rage += value
        console.log("addRage",value,this.rage)
        // 刷新rage progress
        this.rageProgressChange()
        
    }


    onPlayerUltimateReady(){
        this.ndRageBar.getChildByName("RageKuang").active = true;
        this.UltimateSkillButton.interactable = true;
        
        // 移除之前的事件并注册一次性事件
        this.UltimateSkillButton.node.off(Button.EventType.CLICK);
        this.UltimateSkillButton.node.once(Button.EventType.CLICK, this.onUltimateButtonClick, this);
    }
    
    private onUltimateButtonClick = () => {
        if (this.rage >= this.rageMax) {
            this.releaseSkill(this.skill3, BattleManager.Instance.nearestEnemy);
            this.rage = 0;
            this.rageProgressChange()
            this.UltimateSkillButton.interactable = false;
            this.ndRageBar.getChildByName("RageKuang").active = false;
        }
    }

    onAttackAnimationFinished() {
        this.state = EState.Idle;
        this.canAttack = true;
        // 立即检查是否有新敌人可以攻击
        this.checkEnemyInRange();
    }

    onPlayerAPChange(ap: number) {
        this.ap = ap;
    }

    // 应用被动武学的效果

    checkPassiveSkill(){
        switch (this.playerType){
            case EPlayerType.Shaolin:
                if(this.passiveSKill[0]===EPassiveSkill.ShaolinPassive1){
                    console.log("少林被动武学1")
                }
                if(this.passiveSKill[0]===EPassiveSkill.ShaolinPassive2){
                    console.log("少林被动武学2")
                }
                if(this.passiveSKill[0]===EPassiveSkill.ShaolinPassive3){
                    console.log("少林被动武学3")
                }
                break;
                
            case EPlayerType.Tangmen:
                if(this.passiveSKill[0]===EPassiveSkill.TangmenPassive1){
                    console.log("唐门被动武学1")
                }
                if(this.passiveSKill[0]===EPassiveSkill.TangmenPassive2){
                    console.log("唐门被动武学2")
                }
                if(this.passiveSKill[0]===EPassiveSkill.TangmenPassive3){
                    console.log("唐门被动武学3")
                }
                break;
                
            case EPlayerType.Emei:
                if(this.passiveSKill[0]===EPassiveSkill.EmeiPassive1){
                    console.log("峨眉被动武学1")
                }
                if(this.passiveSKill[0]===EPassiveSkill.EmeiPassive2){
                    console.log("峨眉被动武学2")
                }
                if(this.passiveSKill[0]===EPassiveSkill.EmeiPassive3){
                    console.log("峨眉被动武学3")
                }
                break;
                
            case EPlayerType.Gaibang:
                if(this.passiveSKill[0]===EPassiveSkill.GaibangPassive1){
                    console.log("丐帮被动武学1")
                }
                if(this.passiveSKill[0]===EPassiveSkill.GaibangPassive2){
                    console.log("丐帮被动武学2")
                }
                if(this.passiveSKill[0]===EPassiveSkill.GaibangPassive3){
                    console.log("丐帮被动武学3")
                }
                break;
                
            case EPlayerType.Wudang:
                if(this.passiveSKill[0]===EPassiveSkill.WudangPassive1){
                    console.log("武当被动武学1")
                }
                if(this.passiveSKill[0]===EPassiveSkill.WudangPassive2){
                    console.log("武当被动武学2")
                }
                if(this.passiveSKill[0]===EPassiveSkill.WudangPassive3){
                    console.log("武当被动武学3")
                }
                break;
            default:
                break;
        }
    }


    checkEnemyInRange() {
        // 如果正在攻击或不能攻击，则返回
        if (this.state === EState.Attack || !this.canAttack) {
            return;
        }
         

        
        const nearestEnemy = BattleManager.Instance.nearestEnemy??null
        //console.log('this.attackCheckTimer最近敌人', nearestEnemy, nearestEnemy.worldPosition);
        
        // 检查敌人是否有效
        if (!nearestEnemy || !nearestEnemy.active) {
            return};
        
        const em = nearestEnemy.getComponent(EnemyManager);
        if (!em || em.state === EState.Die) return;
        
        // 计算距离（考虑可能的坐标系差异）
        const playerPos = this.node.worldPosition;
        const enemyPos = nearestEnemy.worldPosition;
        const distance = Math.sqrt(
            Math.pow(playerPos.x - enemyPos.x, 2) +
            Math.pow(playerPos.y - enemyPos.y, 2)
        );
        
        // 检查是否在攻击范围内
        if (distance <= this.attackRange) {
            this.autoAttack(nearestEnemy);
        }
    }



    
    private onBeginContact(self: Collider2D, other: Collider2D) {

    }

    private onEndContact(self: Collider2D, other: Collider2D) {

    }

    
    
autoAttack(enemy: Node) {
    if (!this.canAttack || !enemy) return;
    this.canAttack = false;
    this.state = EState.Attack;

    // 如果没有就绪武学，直接返回
    if (this.skillReady.size === 0) {
        this.canAttack = true;
        this.state = EState.Idle;
        return;
    }

    // 优先级：skill3 > skill2 > skill1
    // 直接在每个条件中处理武学释放逻辑
    if (this.skillReady.has(this.skill3) && BattleManager.Instance.autoDazhao && this.rage >= this.rageMax) {
        //console.log("PlayerManager autoAttack: 释放武学3,  BattleManager.Instance.autoDazhao", BattleManager.Instance.autoDazhao);
        this.releaseSkill(this.skill3, enemy);
        // 删除已使用的武学
        this.skillReady.delete(this.skill3); // 删除已使用的武学
        this.skillCDMap.set(this.skill3, this.skills.get(this.skill3).cd); // 加入武学CD列表
        //清空rage
        this.rage = 0;
        this.rageProgressChange()
        //this.ndRageBar.active = false
        this.ndRageBar.getChildByName("RageKuang").active = false;

        return; // 释放完直接结束方法
    } 
    
    if (this.skillReady.has(this.skill2)) {
        //console.log("PlayerManager autoAttack: 释放武学2");
        this.releaseSkill(this.skill2, enemy);
        // 删除已使用的武学
        this.skillReady.delete(this.skill2); // 删除已使用的武学
        this.skillCDMap.set(this.skill2, this.skills.get(this.skill2).cd); // 加入武学CD列表
        return; // 释放完直接结束方法
    } 
    
    if (this.skillReady.has(this.skill1)) {

        this.releaseSkill(this.skill1, enemy);
        // 删除已使用的武学
        this.skillReady.delete(this.skill1); // 删除已使用的武学
        this.skillCDMap.set(this.skill1, this.skills.get(this.skill1).cd); // 加入武学CD列表
        return; // 释放完直接结束方法
    }
}

rageProgressChange(){
    this.rageBar.progress = this.rage / this.rageMax;
    if (this.rage >= this.rageMax) {
        this.rage = this.rageMax;
        this.rageBar.progress = 1;
        this.ndRageBar.getChildByName("RageKuang").active = true;
        this.UltimateSkillButton.interactable = true;
        
        // 移除之前的事件并注册一次性事件
        this.UltimateSkillButton.node.off(Button.EventType.CLICK);
        this.UltimateSkillButton.node.once(Button.EventType.CLICK, this.onUltimateButtonClick, this);
        //Core.Instance.event.emit(EEvent.PlayerUltimateReady);
    }
}
releaseSkill(skillKey: ESkill, enemy: Node) {

    // 增加怒气值
    // this.rage += this.ragePerAttack;
    // this.rageProgressChange()


    // console.log("skillKey", skillKey);
    //this.skillReady.delete(skillKey); // 删除已使用的武学
    //const skill = this.skills.get(skillKey);


    this.direction = enemy.worldPosition.clone().subtract(this.node.worldPosition).normalize()
    //console.log("PlayerManager releaseSkill direction", this.direction);
    const skillNode = ObjectPoolManager.Instance.get(skillKey as EntityType);


    // 设置武学初始位置
    if (this.skills.get(skillKey).skillType === ESkillType.Forward) {
        skillNode.setWorldPosition(this.node.worldPosition);
    } else {
        skillNode.setWorldPosition(enemy.worldPosition);
    }

    // skillNode.setWorldPosition(this.node.worldPosition);

    
    
        // 根据武学类型获取特定管理器
        let sm;
        switch (skillKey) {
            case ESkill.ShaolinSkill1:
                sm = skillNode.getComponent(ShaolinSkill1) || skillNode.addComponent(ShaolinSkill1);
                break;
            case ESkill.ShaolinSkill2:
                sm = skillNode.getComponent(ShaolinSkill2) || skillNode.addComponent(ShaolinSkill2);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                break;
            case ESkill.TangmenSkill1:
                sm = skillNode.getComponent(TangmenSkill1) || skillNode.addComponent(TangmenSkill1);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                break;
            case ESkill.TangmenSkill2:
                sm = skillNode.getComponent(TangmenSkill2) || skillNode.addComponent(TangmenSkill2);
                break;
            case ESkill.TangmenSkill3:
                sm = skillNode.getComponent(TangmenSkill3) || skillNode.addComponent(TangmenSkill3);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                break;
            case ESkill.EmeiSkill1:
                sm = skillNode.getComponent(EmeiSkill1) || skillNode.addComponent(EmeiSkill1);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                break;
            case ESkill.EmeiSkill2:
                sm = skillNode.getComponent(EmeiSkill2) || skillNode.addComponent(EmeiSkill2);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                break;
            case ESkill.EmeiSkill3:
                sm = skillNode.getComponent(EmeiSkill3) || skillNode.addComponent(EmeiSkill3);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                break;
            case ESkill.GaibangSkill1:
                console.log("GaibangSkill1 releaseSkill");
                sm = skillNode.getComponent(GaibangSkill1) || skillNode.addComponent(GaibangSkill1);

                break;
            case ESkill.GaibangSkill2:
                console.log("GaibangSkill2 releaseSkill");
                sm = skillNode.getComponent(GaibangSkill2) || skillNode.addComponent(GaibangSkill2);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                //sm.numOfBullets = this.skills.get(skillKey).numOfBullets || 3; // 获取武学配置的子弹数量
                AudioManager.Instance.playSound("fire");
                break;
            case ESkill.GaibangSkill3:
                console.log("GaibangSkill3 releaseSkill");
                sm = skillNode.getComponent(GaibangSkill3) || skillNode.addComponent(GaibangSkill3);
                break;
            case ESkill.WudangSkill1:
                sm = skillNode.getComponent(WudangSkill1) || skillNode.addComponent(WudangSkill1);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                break;
            case ESkill.WudangSkill2:
                sm = skillNode.getComponent(WudangSkill2) || skillNode.addComponent(WudangSkill2);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                break;
            case ESkill.WudangSkill3:
                sm = skillNode.getComponent(WudangSkill3) || skillNode.addComponent(WudangSkill3);
                sm.isMasterBullet = true; // 标记为主子弹
                sm._hasSplit = false; // 重置分裂状态
                break;
            default:
                sm = skillNode.getComponent(SkillManager) || skillNode.addComponent(SkillManager);
        }
        sm.setDirection(this.direction);
        sm.init(skillKey, this);


}




}


